package com.clever.bean.model;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.web.socket.TextMessage;

/**
 * @Author xixi
 * @Date 2024-04-09 15:30
 **/
public class WebSocketMessagePayload<T> {
    private String type;
    private T content;

    public WebSocketMessagePayload(String type) {
        this.type = type;
    }

    public WebSocketMessagePayload(String type, T content) {
        this.type = type;
        this.content = content;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public TextMessage toTextMessage() {
        return new TextMessage(JSONUtil.toJsonStr(this));
    }

    public static TextMessage getTextMessage(String type) {
        return new TextMessage(JSONUtil.toJsonStr(new WebSocketMessagePayload<String>(type)));
    }

    public static TextMessage getTextMessage(String type, String content) {
        return new TextMessage(JSONUtil.toJsonStr(new WebSocketMessagePayload<String>(type, content)));
    }
}
